macro(add_man_pages _files _man_section)
    foreach (_file ${_files} ${ARGN})
        get_filename_component(_filename ${_file} NAME)
        set(_file.gz ${CMAKE_CURRENT_BINARY_DIR}/${_filename}.gz)

        add_custom_command(
            OUTPUT ${_file.gz}
            COMMAND gzip -c ${_file} > ${_file.gz}
            DEPENDS ${_file}
        )

        install(FILES ${_file.gz} DESTINATION share/man/${_man_section})
    endforeach (_file)

    add_custom_target(docs ALL DEPENDS ${_file.gz})
endmacro(add_man_pages)

file(GLOB mans8 *.8)
add_man_pages(${mans8} man8)
